<template>
  <el-dialog
    :visible.sync="dialogVisible"
    title="辅助计量单位数量"
    modal-append-to-body
    append-to-body
    width="600px"
  >
    <el-table
      :data="quantityInfo"
      border
      stripe
    >
      <el-table-column
        label="单位"
        prop="name"
        align="center"
      />

      <el-table-column
        label="换算比例"
        prop="conversion_ratio"
        align="center"
      />

      <el-table-column
        label="数量"
        prop="quantity"
        align="center"
      >
        <template v-slot="{ row }">
          {{ quantityFormat(row) }}
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script>

export default {
  name: '',
  props: {
    visible: {
      type: Boolean,
      default: false
    },

    quantityInfo: {
      type: Array,
      default: () => []
    },

    quantity: {
      type: Number,
      default: 0
    },

    mainUnit: {
      type: String,
      default: ''
    }
  },
  data() {
    return {

    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(value) {
        this.$emit('update:visible', value)
      }
    }
  },
  methods: {
    quantityFormat(row) {
      const conversionRation = row.conversion_ratio
      const boxes = Math.floor(this.quantity / conversionRation)
      const remaining = Math.round((this.quantity / conversionRation - boxes) * conversionRation)

      return `${boxes}${row.name} ${remaining}${this.mainUnit}`
    }
  }
}
</script>
<style lang="scss" scoped>
.unit-info {
  display: flex;
  justify-content: space-between;
  align-items: end;
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
  &__data {
    width: 45%;
    .label {
      margin-bottom: 10px;
    }
  }

}
</style>
